﻿
Imports osi.root.connector

Public Class unmanaged_pool(Of T)
    Private ReadOnly q As qless(Of pointer(Of T)) = Nothing

    Public Sub New()
        q = New qless(Of pointer(Of T))()
    End Sub

    Public Function [get](ByRef o As T) As Boolean
        Dim p As pointer(Of T) = Nothing
        If q.pop(p) Then
            o = +p
        Else
            o = create()
        End If
        k_assert(Not o Is Nothing)
        Return True
    End Function

    Public Function [get]() As T
        Dim rtn As T = Nothing
        k_assert([get](rtn))
        Return rtn
    End Function

    Public Sub release(ByVal i As T)
        k_assert(Not i Is Nothing)
        q.push(New pointer(Of T)(i))
    End Sub

    Public Sub clear()
        q.clear()
    End Sub

    Protected Overridable Function create() As T
        Return alloc(Of T)()
    End Function

    Protected Overrides Sub Finalize()
        GC.KeepAlive(q)
        MyBase.Finalize()
    End Sub
End Class
